﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using ReflectionHelper;
using Helper;

/// <summary>
/// Summary description for DDLYear
/// </summary>
/// 
namespace Web.UI.CustomControls
{
    public class RoleDropDownList : DropDownList
    {
        private Role selectedRole;
        private List<Role> roles;

        public List<Role> Roles
        {
            get
            {
                return roles;
            }
            set
            {
                roles = value;
            }
        }

        public Role SelectedRole
        {
            get
            {
                if (this.selectedRole!=null)
                {
                    try
                    {
                        this.selectedRole = new Role();
                    }
                    catch(FormatException ex)
                    {
                        throw new FormatException("The must must me an integer value.",ex);
                    }
                }

                return this.selectedRole;
            }
            set
            {

                this.selectedRole= value;
                if(value.RoleID.HasValue)
                {
                    this.SelectedValue = value.RoleID.ToString();
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!this.Page.IsPostBack)
            {
                if (Roles != null)
                {
                    foreach (Role role in Roles)
                    {
                        this.Items.Add(new ListItem(role.Name, Convert.ToString(role.RoleID.Value)));
                    }
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
    }
    
}